<?php

namespace app\controllers;

use app\models\Patients;
use app\models\IcdNineEntries;
use app\models\MedicalTestAnswers;
use app\models\PrescriptionRecords;
use app\models\PsychologicalTestAnswers;
use app\models\PhysicalSymptomAnswers;
use app\models\NeuropsychologicalSymptomAnswers;
use app\models\AnswerOptions;

use lithium\data\model\Query;
use lithium\data\Connections;

use lithium\security\Auth;
use lithium\action\DispatchException;

class PatientsController extends \lithium\action\Controller {

	public function index() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}

		$patients = Patients::all(array(
    		'order' => array('PatID' => 'ASC')));
		return compact('patients');
	}

	public function view() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$patient = Patients::first($this->request->id);

		$icd_nine_entries = IcdNineEntries::all(array(
			'conditions' => array('patient_id' => $patient->id),
			'order' => array('date_given' => 'ASC'),
			'with' => 'IcdCodes'
		));

		$diagnostic_tests = MedicalTestAnswers::all(array(
			'conditions' => array('patient_id' => $patient->id),
			'order' => array('record_date' => 'ASC'),
			'with' => 'MedicalTests'
		));

		$prescriptions = PrescriptionRecords::all(array(
			'conditions' => array('patient_id' => $patient->id),
			'order' => array('start_date' => 'ASC'),
			'with' => 'Medications'
		));

		$psychological_tests = PsychologicalTestAnswers::all(array(
			'conditions' => array('patient_id' => $patient->id),
			'order' => array('record_date' => 'ASC')
		));

		$answer_options = AnswerOptions::all(array(
			'return' => 'array',
			'order' => array('type' => 'ASC')
		));

		foreach ($psychological_tests as $psychological_test) {
			foreach ($answer_options as $answer_option) {
				if ($answer_option['type'] == $psychological_test->qnumber1 && $answer_option['id'] == 8) {
					$psychological_test->description_qnumber1 = $answer_option['description'];
				}
				if ($answer_option['type'] == $psychological_test->qnumber2 && $answer_option['id'] == 9) {
					$psychological_test->description_qnumber2 = $answer_option['description'];
				}
			}
		}

        $physical_symptoms = PhysicalSymptomAnswers::all(array(
        	'conditions' => array('patient_id' => $patient->id),
        	'order' => array('record_date' => 'ASC')
        ));

        $neuropsychological_symptoms = NeuropsychologicalSymptomAnswers::all(array(
        	'conditions' => array('patient_id' => $patient->id),
        	'order' => array('record_date' => 'ASC')
        ));
		return compact('patient', 'icd_nine_entries', 'diagnostic_tests', 'prescriptions', 'psychological_tests', 'physical_symptoms', 'neuropsychological_symptoms');
	}

	public function add() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$patient = Patients::create();

		if (($this->request->data) && $patient->save($this->request->data)) {
			return $this->redirect(array('Patients::view', 'args' => array($patient->id)));
		}
		return compact('patient');
	}

	public function edit() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		$patient = Patients::find($this->request->id);

		if (!$patient) {
			return $this->redirect('Patients::index');
		}
		if (($this->request->data) && $patient->save($this->request->data)) {
			return $this->redirect(array('Patients::view', 'args' => array($patient->id)));
		}
		return compact('patient');
	}

	public function delete() {
		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "Patients::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		Patients::find($this->request->id)->delete();
		return $this->redirect('Patients::index');
	}
}

?>